package com.card.aab.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.card.aab.dao.AabMapper;
import com.card.aab.domain.Aab;
import com.card.aab.domain.AabExample;
import com.card.aab.domain.AabExample.Criteria;
import com.card.aab.service.AabService;
import com.card.framework.mybatis.mapper.BatchCallBack;
import com.card.framework.mybatis.mapper.MyFrameworkSupperMapper;
import com.card.framework.mybatis.optimist.ModifiedByAnotherUserException;
import com.card.framework.page.model.PageModel;
@Service("AabServiceImpl")
public class AabServiceImpl extends MyFrameworkSupperMapper implements AabService{

	@Override
	public Map<String, Object> selectAabForPage(Aab aab, PageModel pageModel) {
		AabMapper mapper = this.getMapper(AabMapper.class);
		AabExample aabExample = new AabExample();
		//AabExample.createCriteria().andUsernameIsNotNull();
		//AabExample.createCriteria().andUsernameEqualTo(Aab.getUsername());
		aabExample.setPage(pageModel);
		if(StringUtils.isNotEmpty(pageModel.getSort())) 
			aabExample.setOrderByClause(pageModel.getSort()+" "+pageModel.getOrder());
//		if(StringUtils.isNotEmpty(aab.getUsername())) 
//		aabExample.createCriteria().andUsernameEqualTo(aab.getUsername());
		
		Criteria criteria = aabExample.createCriteria();
		if(aab.getId() != null)
			criteria.andIdEqualTo(aab.getId());
		if(StringUtils.isNotEmpty(aab.getNamesimp()))
			criteria.andNamesimpLike("%" + aab.getNamesimp() + "%");
		if(StringUtils.isNotEmpty(aab.getTelephone()))
			criteria.andTelephoneLike("%" + aab.getTelephone() + "%");
		if(aab.getCreatdate() != null)
			criteria.andCreatdateEqualTo(aab.getCreatdate());
		criteria.andDelflgEqualTo((byte)1);
		List<Aab> list = mapper.selectByExample(aabExample);
		Map<String, Object> modelMap = new HashMap<String, Object>();  
	    modelMap.put("total", pageModel.getTotalCount());  
	    modelMap.put("rows", list); 
	    return modelMap;
	}

	@Override
	public void deleteAabs(final List<Aab> aab)
			throws ModifiedByAnotherUserException, Exception {
		batchSubmit(AabMapper.class,new BatchCallBack() {
			@Override
			public <E> void batchCallBack(E mapper) {
				if(aab != null && aab.size() > 0) {
					for(Aab a :aab){
						a.setDelflg((byte)0);
						((AabMapper) mapper).updateByPrimaryKeySelective(a);
					}
				}
			}
		});
	}
	@Override
	@Transactional
	public void addOrUpdate(Aab aab) {
		try{
			AabMapper mapper = this.getMapper(AabMapper.class);
			//Aaa queryResult = mapper.selectByPrimaryKey(aaa.getUserid());
			if(aab.getId()==null){
				mapper.insert(aab);
			}else
			{
				AabExample aabExample = new AabExample();
				aabExample.createCriteria().andIdEqualTo(aab.getId());
				mapper.updateByExample(aab, aabExample);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public Aab getById(long id) {
		AabMapper mapper = this.getMapper(AabMapper.class);
	    return mapper.selectByPrimaryKey(id);
	}

}
